package cn.tedu.knows.search.kafka;

import cn.tedu.knows.commons.model.Topic;
import cn.tedu.knows.search.service.IQuestionService;
import cn.tedu.knows.search.vo.QuestionVo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class QuestionConsumer {

    private Gson gson=new Gson();

    @Resource
    private IQuestionService questionService;

    @KafkaListener(topics = Topic.QUESTION_TOPIC)
    public void receive(
            ConsumerRecord<String,String> record){
        //获得信息中的值
        String json=record.value();
        //将获得的json格式字符串转换为java对象
        QuestionVo questionVo=gson.fromJson(
                json,QuestionVo.class);
        log.debug("即将新增问题到ES:{}",questionVo);
        questionService.saveQuestion(questionVo);
    }


}
